// Package controllers
// @Title  adminUser.go
// @Description  管理员控制器
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/library"
	"beego-admin/models"
	"time"
)

var genderMap = map[int]string{0: "未知", 1: "男", 2: "女"}

type AdminUserController struct {
	BaseController
}

// Index 管理员列表页
func (this *AdminUserController) Index() {
	this.TplName = "admin/system_setting/admin_user.tpl"
}

// Add 管理员添加页
func (this *AdminUserController) Add() {
	list, _, _ := models.GetRoleList(9999,1)
	var roleListMap map[int]string
	roleListMap = make(map[int]string)
	for _, v := range *list {
		roleListMap[v.Id] = v.RoleName
	}
	this.Data["role_list"] = roleListMap
	this.TplName = "admin/system_setting/admin_user_add.tpl"
}

// Edit 管理员编辑页
func (this *AdminUserController) Edit() {
	id,_ := this.GetInt("id",0)
	adminUserInfo,_ := models.GetAdminUserInfoById(id)

	list, _, _ := models.GetRoleList(9999,1)
	var roleListMap map[int]string
	roleListMap = make(map[int]string)
	for _, v := range *list {
		roleListMap[v.Id] = v.RoleName
	}

	this.Data["role_list"] = roleListMap
	this.Data["user_info"] = adminUserInfo
	this.TplName = "admin/system_setting/admin_user_edit.tpl"
}



// AjaxGetList ajax获取管理员列表
func (this *AdminUserController) AjaxGetList() {
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	userName := this.GetString("user_name")
	list, count, _ := models.GetAdminUserList(pageSize,page,userName)
	type adminObj struct{
		Id                 int    `json:"id"`
		AdminUser               string `json:"admin_user"`
		PassWord                string `json:"pass_word"`
		RoleName				string `json:"role_name"`
		Gender                int `json:"gender"`
		GenderName                string `json:"gender_name"`
		Mobile                string `json:"mobile"`
		Email                string `json:"email"`
		Remark                string `json:"remark"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]adminObj
	for _, v := range *list {
		genderName := "未知"
		if v.Gender > 0 || v.Gender < 4{
			genderName = genderMap[v.Gender]
		}

		roleInfo,_ := models.GetRoleInfoById(v.RoleId)
		tmpParam := adminObj{v.Id,v.AdminUser, v.PassWord,roleInfo.RoleName,v.Gender,genderName,v.Mobile,v.Email,v.Remark,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}

// AjaxAdd ajax添加管理员
func (this *AdminUserController) AjaxAdd() {
	userName := this.GetString("username")
	passWord := this.GetString("pass_word","123456")
	roleId,_ := this.GetInt("role_id",0)
	gender,_ := this.GetInt("gender",0)
	phone := this.GetString("phone")
	email := this.GetString("email")
	remark := this.GetString("remark")

	phoneCheck := library.VerifyMobileFormat(phone)
	if phoneCheck == false{
		this.Data["json"] = this.AjaxReturn(20002,"手机号格式错误",nil)
		this.ServeJSON()
		this.StopRun()
	}

	if email != ""{
		emailCheck := library.VerifyEmailFormat(email)
		if emailCheck == false{
			this.Data["json"] = this.AjaxReturn(20002,"邮箱格式错误",nil)
			this.ServeJSON()
			this.StopRun()
		}
	}

	result, _ := models.GetAdminUserInfoByAdminName(userName)
	if result.Id > 0{
		this.Data["json"] = this.AjaxReturn(20001,"用户名已存在",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.AddAdminUser(userName,passWord,roleId,gender,phone,email,remark)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

// AjaxEdit ajax更新管理员
func (this *AdminUserController) AjaxEdit() {
	userId,_ := this.GetInt("id",0)
	passWord := this.GetString("pass_word")
	roleId,_ := this.GetInt("role_id",0)
	gender,_ := this.GetInt("gender",0)
	phone := this.GetString("phone")
	email := this.GetString("email")
	remark := this.GetString("remark")

	phoneCheck := library.VerifyMobileFormat(phone)
	if phoneCheck == false{
		this.Data["json"] = this.AjaxReturn(20002,"手机号格式错误",nil)
		this.ServeJSON()
		this.StopRun()
	}

	if email != ""{
		emailCheck := library.VerifyEmailFormat(email)
		if emailCheck == false{
			this.Data["json"] = this.AjaxReturn(20002,"邮箱格式错误",nil)
			this.ServeJSON()
			this.StopRun()
		}
	}
	adminUserInfo,_ := models.GetAdminUserInfoById(userId)
	if adminUserInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(20002,"修改的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if passWord == ""{
		passWord = adminUserInfo.PassWord
	}else{
		passWord = library.Md5V(passWord)
	}

	id := models.UpdateAdminUserById(userId,passWord,roleId,gender,phone,email,remark)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"更新成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"更新失败",nil)
	}

	this.ServeJSON()
}

// AjaxDelete ajax删除管理员
func (this *AdminUserController) AjaxDelete() {
	userId,_ := this.GetInt("id",0)

	if userId == 1{
		this.Data["json"] = this.AjaxReturn(10000,"admin用户不可删除",nil)
		this.ServeJSON()
		this.StopRun()
	}
	adminUserInfo,_ := models.GetAdminUserInfoById(userId)
	if adminUserInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteAdminUserById(userId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}


